package com.pms.controller;

import com.pms.common.PageBean;
import com.pms.pojo.WorkAttendance;
import com.pms.pojo.statistics.*;
import com.pms.result.Result;
import com.pms.service.WorkAttendanceService;
import com.pms.util.JwtUtils;


import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/attendance")
@Slf4j
public class WorkAttendanceController {

    @Autowired
    private WorkAttendanceService workAttendanceService;

    @GetMapping
    public Result<PageBean<WorkAttendance>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String userName,
            Integer operateType,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {

        log.info("查询考勤记录，参数：page={}, pageSize={}, userName={}, operateType={}, startTime={}, endTime={}",
                page, pageSize, userName, operateType, startTime, endTime);

        PageBean<WorkAttendance> pageBean = workAttendanceService.page(page, pageSize,
                userName, operateType, startTime, endTime);
        return Result.success(pageBean);
    }

    @GetMapping("/{id}")
    public Result<WorkAttendance> getById(@PathVariable Long id) {
        log.info("根据ID查询考勤记录：{}", id);
        WorkAttendance workAttendance = workAttendanceService.getById(id);
        return Result.success(workAttendance);
    }

    @PutMapping
    public Result<Void> update(@RequestBody WorkAttendance workAttendance) {
        log.info("更新考勤记录：{}", workAttendance);
        workAttendanceService.update(workAttendance);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除考勤记录：{}", id);
        workAttendanceService.delete(id);
        return Result.success();
    }

    @PostMapping("/check-in")
    public Result<Void> checkIn(@RequestBody WorkAttendance workAttendance) {
        log.info("员工打卡：{}", workAttendance);
        workAttendanceService.checkIn(workAttendance);
        return Result.success();
    }

    @GetMapping("/user/{userId}")
    public Result<List<WorkAttendance>> getByUserId(@PathVariable Long userId) {
        log.info("根据用户ID查询考勤记录：{}", userId);
        try {
            List<WorkAttendance> records = workAttendanceService.getByUserId(userId);
            log.info("查询到记录数：{}", records.size());
            return Result.success(records);
        } catch (Exception e) {
            log.error("查询考勤记录失败", e);
            return Result.error("查询考勤记录失败");
        }
    }

    /**
     * 获取考勤统计数据
     */
    @GetMapping("/statistics")
    public Result<AttendanceStatistics> getStatistics(AttendanceStatisticsQuery query) {
        log.info("获取考勤统计数据，查询参数：{}", query);
        try {
            AttendanceStatistics statistics = workAttendanceService.getAttendanceStatistics(query);
            return Result.success(statistics);
        } catch (Exception e) {
            log.error("获取考勤统计数据失败", e);
            return Result.error("获取考勤统计数据失败");
        }
    }

    /**
     * 获取每日考勤统计数据
     */
    @GetMapping("/statistics/daily")
    public Result<List<DailyAttendance>> getDailyStatistics(AttendanceStatisticsQuery query) {
        log.info("获取每日考勤统计数据，查询参数：{}", query);
        try {
            List<DailyAttendance> dailyStats = workAttendanceService.getDailyAttendanceStatistics(query);
            return Result.success(dailyStats);
        } catch (Exception e) {
            log.error("获取每日考勤统计数据失败", e);
            return Result.error("获取每日考勤统计数据失败");
        }
    }

    /**
     * 获取部门考勤统计数据
     */
    @GetMapping("/statistics/department")
    public Result<List<DepartmentAttendance>> getDepartmentStatistics(AttendanceStatisticsQuery query) {
        log.info("获取部门考勤统计数据，查询参数：{}", query);
        try {
            List<DepartmentAttendance> deptStats = workAttendanceService.getDepartmentAttendanceStatistics(query);
            return Result.success(deptStats);
        } catch (Exception e) {
            log.error("获取部门考勤统计数据失败", e);
            return Result.error("获取部门考勤统计数据失败");
        }
    }

    /**
     * 获取考勤详细记录
     */
    @GetMapping("/statistics/records")
    public Result<PageBean<AttendanceRecord>> getAttendanceRecords(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            AttendanceStatisticsQuery query) {
        log.info("获取考勤详细记录，页码：{}，每页记录数：{}，查询参数：{}", page, pageSize, query);
        try {
            PageBean<AttendanceRecord> records = workAttendanceService.getAttendanceRecords(page, pageSize, query);
            return Result.success(records);
        } catch (Exception e) {
            log.error("获取考勤详细记录失败", e);
            return Result.error("获取考勤详细记录失败");
        }
    }

}